package com.hxjr.dialog.adapter;

import android.content.Context;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public abstract class SimpleBaseAdapter<T> extends BaseAdapter
{

	protected Context context;
	protected List<T> data;

	public SimpleBaseAdapter(Context context, List<T> data)
	{
		this.context = context;
		this.data = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
	}

	@Override
	public int getCount()
	{
		return data == null ? 0 : data.size();
	}

	@Override
	public Object getItem(int position)
	{
		if (position >= data.size())
			return null;
		return data.get(position);
	}

	@Override
	public long getItemId(int position)
	{
		return position;
	}

	/**
	 * 改方法需要子类实现，需要返回item布局的resource id
	 * 
	 * @return
	 */
	public abstract int getItemResource();

	/**
	 * 使用该getItemView方法替换原来的getView方法，需要子类实现
	 * 
	 * @param position
	 * @param convertView
	 * @param holder
	 * @return
	 */
	public abstract View getItemView(int position, View convertView,
			ViewHolder holder);

	@SuppressWarnings("unchecked")
	@Override
	public View getView(int position, View convertView, ViewGroup parent)
	{
		ViewHolder holder;
		if (null == convertView)
		{
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			convertView = inflater.inflate(getItemResource(), parent, false);
			// convertView = View.inflate(context, getItemResource(), false);
			holder = new ViewHolder(convertView);
			convertView.setTag(holder);
		} else
		{
			holder = (ViewHolder) convertView.getTag();
		}
		return getItemView(position, convertView, holder);
	}

	public class ViewHolder
	{
		private SparseArray<View> views = new SparseArray<View>();
		private View convertView;

		public ViewHolder(View convertView)
		{
			this.convertView = convertView;
		}

		@SuppressWarnings("unchecked")
		public <T extends View> T getView(int resId)
		{
			View v = views.get(resId);
			if (null == v)
			{
				v = convertView.findViewById(resId);
				views.put(resId, v);
			}
			return (T) v;
		}
	}

	public void setData(List<T> elem)
	{
		this.data = elem;
		notifyDataSetChanged();
	}

	public void addData(T elem)
	{
		data.add(data.size(), elem);
		notifyDataSetChanged();
	}

	public void addAll(List<T> elem)
	{
		data.addAll(elem);
		notifyDataSetChanged();
	}

	public void refreshData(List<T> data)
	{
		this.data = data;
		notifyDataSetChanged();
	}

	public void remove(T elem)
	{
		data.remove(elem);
		notifyDataSetChanged();
	}

	public void remove(int index)
	{
		data.remove(index);
		notifyDataSetChanged();
	}

	public void replaceAll(List<T> elem)
	{
		data.clear();
		data.addAll(elem);
		notifyDataSetChanged();
	}
}